package com.example.springstudy;

import com.example.springstudy.myEventBus.StringPublisherEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * @Auther ZhaoJunZhou
 * @Date 2020/7/22
 *
 * {@link ApplicationRunner} 代表在项目启动完成之后进行调用
 */
@Component
public class run implements ApplicationRunner {
    /**
     * {@link ApplicationContext} 继承了
     * {@link ApplicationEventPublisher}
     * 为此可以调用{@link ApplicationEventPublisher#publishEvent(Object)} 方法
     * 来提交事件之后会交由到监听类型为{@link StringPublisherEvent}中进行执行
     * 在这里的监听类为{@link com.example.springstudy.myEventBus.SecondListner}
     * 交由当中的{@link com.example.springstudy.myEventBus.SecondListner#onApplicationEvent(StringPublisherEvent)}
     * 进行执行
     *
     */
    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        applicationContext.publishEvent(new StringPublisherEvent(this, "成功"));
        applicationContext.publishEvent(new StringPublisherEvent(this, "成功"));

    }
}
